package blog

import (
	"encoding/json"
	"net/http"
	"os"

	"github.com/gin-gonic/gin"
)

type HotTopicJSON struct {
	HotTopics []string `json:"hot_topics"`
}

func GetHotTopics(c *gin.Context) {
	file, err := os.Open(hotTopicPath)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code" : 4001, "data" : "读取热度topic失败"})
		return
	}
	defer file.Close()
	var hotTopics HotTopicJSON
	decoder := json.NewDecoder(file)
	err = decoder.Decode(&hotTopics)

	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code" : 4001, "data" : "解析热度topic失败"})
		return
	}
	
	c.JSON(http.StatusOK, gin.H{"code" : 200, "data" : hotTopics.HotTopics})
}